<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Repositories\ContractRepository;
use App\Repositories\CustomerRepository;
use App\Http\Requests\ContractRequest;
use App\Repositories\OrderRepository;

class ContractController extends AdminController
{
    public function __construct(ContractRepository $contract,  CustomerRepository $customer, OrderRepository $order)
    {
        $this->contract = $contract;
        $this->customer = $customer;
        $this->order    = $order;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->has('cid')) {
            $cid = $request->input('cid');
            $customer = $this->customer->find($cid);
            $contracts = $this->contract->findAllBy('cid', $cid);
            return view('admin.contract.list', compact('contracts', 'cid', 'customer'));
        }

        $where = collect($request->except('page'));
        $data = $this->contract->paginateList($where);

        return view('admin.contract.index', compact('data', 'where'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        if ($request->has('cid')) {
            $customer = $this->customer->find($request->input('cid'));
        } else {
            $customer = null;
        }

        $tpl = $request->has('pop') ? 'admin.contract.create_pop' : 'admin.contract.create';

        return view($tpl, compact('customer'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ContractRequest $request)
    {
        $result = $this->contract->create($request->all());

        if ($request->ajax()) {
            return $result ? $result->contract_no : '';
        }

        $url = 'admin/contract?cid='.$request->input('cid');

        if ($result) {
            return redirect($url)->with('success', '合同/发票添加成功');
        }

        return redirect($url)->with('error', '合同/发票添加失败');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request, $id)
    {
        $data = $this->contract->find($id);

        $tpl = 'admin.contract.show';

        if ($request->has('open')) {
            $tpl = 'admin.contract.show_window'; 
        }

        return view($tpl, compact('data'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = $this->contract->find($id);

        return view('admin.contract.edit', compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if (!$request->has('contract')) {
            $request->merge(['contract' => array()]);
        }

        if (!$request->has('invoice_receipt')) {
            $request->merge(['invoice_receipt' => array()]);
        }

        $result = $this->contract->updateFind($request->all(), $id);

        if ($result) {
            return back()->with('success', '合同/发票更新成功');
        }

        return back()->with('error', '合同/发票更新失败');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = $this->contract->find($id);

        if ($data) {
            if ($data->order) {
                flash('error', '此合同包含订单，不允许删除。');
                return response()->json(['code' => 500]); 
            } 
            if ($data->renew) {
                flash('error', '此合同包含订单续费，不允许删除。');
                return response()->json(['code' => 500]); 
            }  
            
            $result = $data->delete($id);

            if ($result) {
                flash('success', '该合同删除成功');
                return response()->json(['code' => 200]);
            }

            flash('error', '该合同删除失败');
            return response()->json(['code' => 500]);

        }

        flash('error', '该合同不存在，请检查后再尝试。');
        return response()->json(['code' => 500]);
    }

    // 根据客户ID查询合同号
    public function getContractsByCustomer($cid)
    {
        return $this->contract->findAllBy('cid', $cid);
    }

}
